<script setup lang="tsx">
import { Form, FormSchema } from '@/components/Form'
import { useForm } from '@/hooks/web/useForm'
import { reactive, watch, inject, Ref, ref, onBeforeUnmount, onMounted } from 'vue'
import { useValidator } from '@/hooks/web/useValidator'
import { Row } from '@/api/bonus'
import { searchStoreApi } from '@/api/utils/search'
import { ElCheckboxGroup, ElCheckbox, ElInput, ElButton, ElMessage } from 'element-plus'
const { required } = useValidator()
const currentRow: Ref<Row> = inject('currentRow') as Ref<Row>
const { formRegister, formMethods } = useForm()
const { setValues, getFormData, getElFormExpose } = formMethods
const store_ids = ref([])
const name = ref('')
onMounted(() => {
  getListApidata('')
})
const options = ref<Array<{ name: string; id: number }>>([])
const getListApidata = async (name) => {
  const res = await searchStoreApi({ field: 'id,name,name,salesman_id', name: name })
  options.value = res.data.list
}
const search = () => {
  if (name.value) {
    getListApidata(name.value)
  } else {
    ElMessage('请输入搜索内容')
  }
}
const Reset = () => {
  getListApidata('')
  name.value = ''
}
const schema = reactive<FormSchema[]>([
  {
    field: 'real_name',
    label: '业务员姓名',
    component: 'Input',
    formItemProps: {
      rules: [required(), { min: 1, max: 30, message: '最多输入30个字符', trigger: 'blur' }]
    },
    componentProps: {
      placeholder: '请输入(最多30个字符)'
    }
  },
  {
    field: 'mobile',
    label: '电话',
    component: 'Input',
    formItemProps: {
      rules: [
        required(),
        { min: 11, max: 11, message: '请输入电话', trigger: 'blur' },
        {
          pattern: /^[1][3,4,5,6,7,8,9][0-9]{9}$/,
          message: '请输入电话',
          trigger: 'blur'
        }
      ]
    }
  },
  {
    field: '',
    label: '',
    component: 'Divider',
    colProps: { span: 28 }
  }
])

const submit = async () => {
  const elForm = await getElFormExpose()
  const valid = await elForm?.validate().catch((err) => {
    console.log(err)
  })
  if (valid) {
    const formData = await getFormData()
    const formDataList = {
      id: formData.id || '',
      real_name: formData.real_name || '',
      mobile: formData.mobile || '',
      money: formData.money || '',
      store_ids: store_ids.value.join(',') || ''
    }
    return formDataList
  }
}

const reset = async () => {
  const elForm = await getElFormExpose()
  elForm?.resetFields()
}

watch(
  () => currentRow.value,
  (currentRow) => {
    if (!currentRow) return
    setValues(currentRow)
  },
  {
    deep: true,
    immediate: true
  }
)

onBeforeUnmount(reset)

defineExpose({
  submit,
  reset
})
</script>

<template>
  <Form @register="formRegister" :schema="schema" />
  <div>
    <div style="display: inline-block; margin-right: 20px">业务范围:</div>
    <div style="display: inline-block; margin-right: 20px"
      ><ElInput v-model="name" style="width: 240px" placeholder="请输入"
    /></div>
    <div style="display: inline-block; margin-right: 20px"
      ><ElButton type="primary" @click="search">搜索</ElButton></div
    >
    <div style="display: inline-block"><ElButton @click="Reset">重置</ElButton></div>
  </div>
  <div style="width: 300px; margin-left: 100px; margin-top: 10px">
    <ElCheckboxGroup v-model="store_ids">
      <ElCheckbox v-for="res in options" :key="res.id" :label="res.id" :value="res.id">
        {{ res.name }}
      </ElCheckbox>
    </ElCheckboxGroup>
  </div>
</template>
